package com.example.mediamoudle.video


import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.SeekBar
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.mediamoudle.R
import com.shuyu.gsyvideoplayer.utils.CommonUtil
import com.shuyu.gsyvideoplayer.utils.Debuger
import com.shuyu.gsyvideoplayer.video.NormalGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer


/**
 * 进度图小图预览的另类实现
 * Created by shuyu on 2016/12/10.
 */
class PreViewGSYVideoPlayer : NormalGSYVideoPlayer {
    private var mPreviewLayout: RelativeLayout? = null
    private var mPreView: ImageView? = null

    //是否因为用户点击
    private var mIsFromUser = false

    /**
     * 如果是需要进度条预览的设置打开，默认关闭
     */
    //是否打开滑动预览
    var isOpenPreView = true
    private var mPreProgress = -2

    /**
     * 1.5.0开始加入，如果需要不同布局区分功能，需要重载
     */
    constructor(context: Context?, fullFlag: Boolean?) : super(context, fullFlag) {}
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}

    override fun init(context: Context) {
        super.init(context)
        initView()
    }

    private fun initView() {
        mPreviewLayout = findViewById<View>(R.id.preview_layout) as RelativeLayout
        mPreView = findViewById<View>(R.id.preview_image) as ImageView
    }

    override fun getLayoutId(): Int {
        return R.layout.video_layout_preview
    }

    override fun prepareVideo() {
        super.prepareVideo()
    }

    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        super.onProgressChanged(seekBar, progress, fromUser)
        if (fromUser && isOpenPreView) {
            val width = seekBar.width
            val time = (progress * duration / 100).toLong()
            val offset = (width - resources.getDimension(R.dimen.seek_bar_image) / 2).toInt() / 100 * progress
            Debuger.printfError("***************** $progress")
            Debuger.printfError("***************** $time")
            showPreView(mOriginUrl, time)
            val layoutParams = mPreviewLayout!!.layoutParams as RelativeLayout.LayoutParams
            layoutParams.leftMargin = offset
            //设置帧预览图的显示位置
            mPreviewLayout!!.layoutParams = layoutParams
            if (mHadPlay && isOpenPreView) {
                mPreProgress = progress
            }
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        super.onStartTrackingTouch(seekBar)
        if (isOpenPreView) {
            mIsFromUser = true
            mPreviewLayout!!.visibility = VISIBLE
            mPreProgress = -2
        }
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        if (isOpenPreView) {
            if (mPreProgress >= 0) {
                seekBar.progress = mPreProgress
            }
            super.onStopTrackingTouch(seekBar)
            mIsFromUser = false
            mPreviewLayout!!.visibility = GONE
        } else {
            super.onStopTrackingTouch(seekBar)
        }
    }

    override fun setTextAndProgress(secProgress: Int) {
        if (mIsFromUser) {
            return
        }
        super.setTextAndProgress(secProgress)
    }

    override fun startWindowFullscreen(context: Context, actionBar: Boolean, statusBar: Boolean): GSYBaseVideoPlayer {
        val gsyBaseVideoPlayer = super.startWindowFullscreen(context, actionBar, statusBar)
        val customGSYVideoPlayer = gsyBaseVideoPlayer as PreViewGSYVideoPlayer
        customGSYVideoPlayer.isOpenPreView = isOpenPreView
        return gsyBaseVideoPlayer
    }

    override fun onPrepared() {
        super.onPrepared()
        startDownFrame(mOriginUrl)
    }

    private fun showPreView(url: String, time: Long) {
        val width = CommonUtil.dip2px(context, 150f)
        val height = CommonUtil.dip2px(context, 100f)
        mPreView?.let {
            Glide.with(context.applicationContext)
                .setDefaultRequestOptions(
                    RequestOptions() //这里限制了只从缓存读取
                        .onlyRetrieveFromCache(true)
                        .frame(1000 * time)
                        .override(width, height)
                        .dontAnimate()
                        .centerCrop()
                )
                .load(url)
                .into(it)
        }
    }

    private fun startDownFrame(url: String) {
        for (i in 1..100) {
            val time = (i * duration / 100).toLong()
            val width = CommonUtil.dip2px(context, 150f)
            val height = CommonUtil.dip2px(context, 100f)
            Glide.with(context.applicationContext)
                .setDefaultRequestOptions(
                    RequestOptions()
                        .frame(1000 * time)
                        .override(width, height)
                        .centerCrop()
                )
                .load(url).preload(width, height)
        }
    }
}
